var init = function(){
    loadUserList();
    $("a#addButton").click(function(){
        $("input#iUName").val("");
        $("input#iUPassword").val("");
        $("input#iUMail").val("");
    });
    $("button#saveButton").click(function(){
        addUser();
    });
}

function loadUserList(){
    $('#usertable tbody').find("tr").remove();
    $.ajax({
        dataType: "json",
        url: "/WeatherAppFrontend/webresources/user/",
        method: "get",
        success: function(data,status,xhr){
            var tmpFrontendUser = data.frontendUser;
            fillUserTable(tmpFrontendUser);
        }
    });
}

function removeUser(id){
    $.ajax({
        dataType: "text",
        url: "/WeatherAppFrontend/webresources/user/" + id,
        method: "delete",
        success: function(data,status,xhr){
            loadUserList();
        },
        error: function(xhr,status,err){
            if(xhr.status == 403){//
                alert("Der letzte Admin darf nicht gelöscht werden!");
            }else{
                alert("Ein ungeahnter Fehler ist aufgetreten: " + xhr.statusText);
            }
        }
    });
}

function fillUserTable(users){
    if((typeof(users) == "object")){
        if(users instanceof Array){
            for(var i = 0; i < users.length; i++){
                var className = "label-default";
                if(users[i].userRole == "ADMIN"){
                    className = "label-important";
                }
                $('#usertable tbody').append('<tr><td>' + users[i].name +'</td><td>' + users[i].email +'</td><td><span class="label '+className+'">' + users[i].userRole +'</span></td><td><a class="btn"><i class="icon-edit"></i> Bearbeiten</a></td><td><a id="user_' + users[i].id + '" class="btn btn-danger deleteBtn"><i id="user_' + users[i].id + '" class="icon-remove icon-white"></i> Entfernen</a></td></tr>');
            }
        }else{
            var className = "label-default";
            if(users.userRole == "ADMIN"){
                className = "label-important";
            }
            $('#usertable tbody').append('<tr><td>' + users.name +'</td><td>' + users.email +'</td><td><span class="label '+className+'">' + users.userRole +'</span></td><td><a class="btn"><i class="icon-edit"></i> Bearbeiten</a></td><td><a id="user_' + users.id + '" class="btn btn-danger deleteBtn"><i id="user_' + users.id + '" class="icon-remove icon-white"></i> Entfernen</a></td></tr>');
        }
    }
    $("a.deleteBtn").click(function(e) {
        var tmpId = e.target.id;
        console.log(tmpId);
        tmpId = tmpId.split("_")[1];
        console.log(tmpId);
        removeUser(tmpId);
    });
}

function addUser(){
    var putData = {
        name : $("input#iUName").val(),
        password : $("input#iUPassword").val(),
        email : $("input#iUMail").val(),
        userRole : $("#iUSelect").val()
    };
    
    $.ajax({
        contentType: "application/json",
        dataType: "text",
        url: "/WeatherAppFrontend/webresources/user/",
        method: "put",
        data : JSON.stringify(putData),
        success: function(data,status,xhr){
            console.log(data);
            $("div#addUserModal").modal('hide');
            loadUserList();
        }
    });
}